﻿using Furion.DynamicApiController;
using Furion.UnifyResult;
using Microsoft.AspNetCore.Mvc;
using MSEAP.Entitys;
using MSEAP.VO.User;
using SqlSugar;

namespace MSEAP.Controllers
{
    [DynamicApiController]
    [ApiDescriptionSettings(Order = 999)]
    public class UserController
    {
        private readonly ILogger<UserController> logger;
        private readonly ISqlSugarClient db;

        public UserController(ILogger<UserController> logger, ISqlSugarClient db)
        {
            this.db = db;
            this.logger = logger;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="uifd">参照下方Schemas中：UserLoginFormDto</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Login([FromBody] LoginVO vo)
        {
            UserEntity user = await db.Queryable<UserEntity>()
                .Where(um => um.IsActive == 1)
                .Where(um => um.UserName == vo.UserName)
                .SingleAsync();
            if (user == null)
            {
                return new JsonResult(new RESTfulResult<object>
                {
                    StatusCode = -1,
                    Succeeded = false,
                    Data = null,
                    Errors = "该账户不存在",
                    Extras = UnifyContext.Take(),
                    Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                });
            }

            if (!user.Password.Equals(vo.Password))
            {
                return new JsonResult(new RESTfulResult<object>
                {
                    StatusCode = -1,
                    Succeeded = false,
                    Data = null,
                    Errors = "密码错误",
                    Extras = UnifyContext.Take(),
                    Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                });
            }

            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = user,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }
    }
}
